<?php

namespace App\Admin\Controllers\Cms;

use App\Admin\Controllers\BaseAdminController;
use App\Models\Cms\QuestionAnswer;
use App\Models\Cms\QuestionCategory;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;

class QuestionAnswerController extends BaseAdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(QuestionAnswer::with(['questionCategory', 'creator']), function (Grid $grid) {
            $grid->enableDialogCreate();
            $grid->disableEditButton();
            $grid->showQuickEditButton('编辑');
            $grid->setDialogFormDimensions('80%', '90%');

            $grid->model()->orderBy('order');

            $grid->column('id')->sortable();
            $grid->column('question_category.name');
            $grid->column('question');
//            $grid->column('answer');
            $grid->column('status')->switch();
            $grid->column('creator.name');
            $grid->column('order')->orderable();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();

                $filter->equal('question_category_id')->select(QuestionCategory::where('status', 1)->pluck('name', 'id'))->width('300px');
                $filter->like('name')->width('300px');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new QuestionAnswer(), function (Show $show) {
            $show->field('id');
            $show->field('question_category_id');
            $show->field('question');
            $show->field('answer');
            $show->field('status');
            $show->field('creator_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new QuestionAnswer(), function (Form $form) {
            $form->display('id');
            $form->select('question_category_id')->options(QuestionCategory::where('status', 1)->pluck('name', 'id'));
            $form->text('question');
            $form->ueditor('answer');
            $form->switch('status')->default('1');
            $form->hidden('creator_id')->default(Admin::user()->id);
        });
    }
}
